<template>
  <div class="search">
    <!-- 位置信息 -->
    <div class="location bottom-gray-line">
      <div class="city" @click="cityClick">{{ currentCity.cityName }}</div>
      <div class="position" @click="positionClick">
        <span>我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="" />
      </div>
    </div>

    <!-- 日期范围 -->
    <div
      class="date-range section bottom-gray-line"
      @click="showCalender = true"
    >
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time">{{ startDatestr }}</span>
        </div>
        <div class="stay">共{{ stayDays }}晚</div>
      </div>
      <div class="end">
        <div class="date">
          <span>离店</span>
          <span>{{ endDatestr }}</span>
        </div>
      </div>
    </div>

    <!-- 日期区间 -->
    <van-calendar
      v-model:show="showCalender"
      type="range"
      @confirm="onConfirm"
      color="#ff8654"
      :round="false"
      :show-confirm="false"
    />

    <!-- 价格/人数 -->
    <div class="item price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>

    <!-- 关键字 -->
    <div class="item keyword bottom-gray-line">关键字/位置/民宿名</div>

    <!-- 热门建议 -->
    <div class="item hot-suggest">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div
          class="item"
          :style="{
            color: item.tagText.color,
            background: item.tagText.background.color,
          }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>

    <!-- 搜索按钮 -->
    <div class="item search-btn">
      <div class="btn" @click="homeSearchClick">开始搜索</div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import useCityStore from "@/stores/modules/city";
import { storeToRefs } from "pinia";
import { ref } from "@vue/reactivity";
import { formateMonthDay, formateStayDays } from "@/utils/formate_date";
const router = useRouter();
import useHomeStore from "@/stores/modules/home";
import useMainStore from "@/stores/modules/main";
import { computed } from "vue";

// 6.定义props:接受从home组件传递过来的数据
// defineProps({
//   hotSuggests: {
//     type: Array,
//     default: () => [],
//   },
// });
const homeStore = useHomeStore();
const { hotSuggests } = storeToRefs(homeStore);

// 1.找到位置/城市
const positionClick = () => {
  navigator.geolocation.getCurrentPosition(
    (res) => {
      console.log("获取位置成功", res);
    },
    (err) => {
      console.log("获取位置失败", err);
    },
    {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0,
    }
  );
};

// 2.跳转到点击城市
const cityClick = () => {
  router.push("/city");
};

// 3.从city.vue页面跳转到home页面，将currentCity显示在页面上
const cityStore = useCityStore();
const { currentCity } = storeToRefs(cityStore);
// console.log(currentCity);

// 4.处理日期时间
// const nowDate = new Date();
// console.log(nowDate);
// const startDate = ref(formateMonthDay(nowDate));
// console.log(startDate);
// const newDate = nowDate.getTime() + 24 * 60 * 60 * 1000;
// const newDate = new Date().setDate(nowDate.getDate() + 1);
// const endDate = ref(formateMonthDay(newDate));
// const stayDays = ref(formateStayDays(nowDate, newDate));
// console.log(stayDays);
// console.log(new Date(newDate));

// 4.1在mainStore中引入
const mainStore = useMainStore();
const { startDate, endDate } = storeToRefs(mainStore);

const startDatestr = computed(() => formateMonthDay(startDate.value));
const endDatestr = computed(() => formateMonthDay(endDate.value));

const stayDays = ref(formateStayDays(startDate.value, endDate.value));

// 5.日历
const showCalender = ref(false);
const onConfirm = (value) => {
  // console.log(value);
  // 5.1 设置日期
  const selectedStartDate = value[0];
  const selectedEndDate = value[1];

  // startDate.value = formateMonthDay(selectedStartDate);
  // endDate.value = formateMonthDay(selectedEndDate);
  // console.log(selectedStartDate, selectedEndDate);

  mainStore.startDate = selectedStartDate;
  mainStore.endDate = selectedEndDate;

  stayDays.value = formateStayDays(selectedStartDate, selectedEndDate);

  // 5.2 关闭弹框
  showCalender.value = false;
};

// 7.首页搜索
const homeSearchClick = () => {
  router.push({
    path: "/search",
    query: {
      startDate: startDate.value,
      endDate: endDate.value,
      currentCity: currentCity.value.cityName,
    },
  });
};
</script>

<style lang="less" scoped>
// 日历组件的高度
.search {
  --van-calendar-popup-height: 100%;
}
.location {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;

  .city {
    flex: 1;
    color: #333;
  }

  .position {
    display: flex;
    width: 74px;
    align-items: center;
    color: #666;
    span {
      font-size: 12px;
      position: relative;
      top: 3px;
    }
    img {
      margin-left: 3px;
      width: 18px;
      height: 18px;
    }
  }
}

.section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999;

  .start {
    flex: 1;
    display: flex;
    height: 44px;
    align-items: center;
  }

  .end {
    min-width: 30%;
    padding-left: 20px;
    color: #333;
  }

  .date {
    display: flex;
    flex-direction: column;

    .tip {
      font-size: 12px;
      color: #999;
    }

    .time {
      margin-top: 3px;
      color: #333;
      font-size: 15px;
      font-weight: 500;
    }
  }
}

.date-range {
  height: 44px;
  .stay {
    flex: 1;
    text-align: center;
    font-size: 12px;
    color: #666;
  }
}

.price-counter {
  height: 44px;
  .start {
    border-right: 1px solid #f2f2f2;
  }
}

.keyword {
  height: 44px;
}

.hot-suggest {
  margin: 10px 0;

  .tag {
    font-size: 12px;
    padding: 4px 8px;
    margin: 3px;
    border-radius: 14px;
    color: #3f4954;
    background-color: #f1f3f5;
  }
}

.search-btn {
  .btn {
    width: 342px;
    height: 38px;
    max-height: 50px;
    font-weight: 500;
    font-size: 18px;
    line-height: 38px;
    text-align: center;
    border-radius: 20px;
    color: #fff;
    background-image: var(--theme-linear-gradient);
  }
}
.item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999;

  .start {
    flex: 1;
    display: flex;
    height: 44px;
    align-items: center;
  }

  .end {
    min-width: 30%;
    padding-left: 20px;
  }

  .date {
    display: flex;
    flex-direction: column;

    .tip {
      font-size: 12px;
      color: #999;
    }

    .time {
      margin-top: 3px;
      color: #333;
      font-size: 15px;
      font-weight: 500;
    }
  }
}

.hot-suggest {
  margin: 10px 0;
  .item {
    padding: 4px 8px;
    border-radius: 12px;
    margin: 3px;
    color: #3f4954;
    background-color: #f1f3f5;
    font-size: 12px;
  }
}
</style>
